package com.ml.tools;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.ml.view.R;

import java.util.ArrayList;
import java.util.Locale;

//import android.support.v4.app.FragmentActivity;

public class BaseActivity extends FragmentActivity implements OnClickListener, OnScrollListener {

    public static Context context;
    public static BaseActivity instance;
    protected static String sdkUrl;
    protected static ArrayList<Activity> ActivityManager = new ArrayList<Activity>();
    public boolean mHandleProgressExternal = false;
    public View mProgressView;
    public LinearLayout progressBar = null;
    public LinearLayout notice = null;
    public TextView title;
    public ImageButton btn_back;
    public Handler mHandler = new Handler();
    public SharedPreferences share;
    public TextView tv_pb;
    boolean flag = true;
    private Animation animShow;
    private Animation animHide;
    private TextView tv_notice;

    private void initAnim() {
        animShow = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1);
        animShow.setDuration(1000);

        animHide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 0);
        animHide.setDuration(1000);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.add(this);
        instance = this;
        initAnim();
        context = this;
        share = getSharedPreferences("config", MODE_PRIVATE);
    }

    public void showProgressBar() {
        runOnUiThread(new Runnable() {
            public void run() {
                if (progressBar == null) {
                    progressBar = createProgressBar(BaseActivity.this, null);
                }
                progressBar.setVisibility(View.VISIBLE);
            }
        });
    }

    public void showProgressBar(final String text) {
        runOnUiThread(new Runnable() {
            public void run() {
                if (progressBar == null) {
                    progressBar = createProgressBar(BaseActivity.this, null);
                }
                tv_pb.setText(text);
                progressBar.setVisibility(View.VISIBLE);
            }
        });
    }

    @SuppressLint("InflateParams")
    public LinearLayout createProgressBar(Activity activity, Drawable customIndeterminateDrawable) {
        FrameLayout rootContainer = (FrameLayout) activity.findViewById(android.R.id.content);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL;

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.pb, null);
        progressBar = (LinearLayout) view.findViewById(R.id.result);
        progressBar.setLayoutParams(lp);
        tv_pb = (TextView) view.findViewById(R.id.result_tv);
        rootContainer.addView(view);
        return progressBar;
    }

    public void hideProgressBar() {
        runOnUiThread(new Runnable() {
            public void run() {
                if (progressBar == null) {
                    progressBar = createProgressBar(BaseActivity.this, null);
                }
                progressBar.setVisibility(View.GONE);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public void startActivity(Intent intent) {
        super.startActivity(intent);
    }

    public boolean needLogin2() {
        SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("token", "");
        return phone.equals("");
    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void showToast(final String text) {
        runOnUiThread(new Runnable() {
            public void run() {
                Toast toastCustom = new Toast(context);
                TextView tv = new TextView(context);
                tv.setText(text);
                tv.setTextSize(20f);
                tv.setPadding(10, 2, 10, 2);
                tv.setGravity(Gravity.CENTER);
                tv.setTextColor(context.getResources().getColor(R.color.white));
                tv.setBackgroundResource(R.drawable.bg_toast);
                toastCustom.setView(tv);
                toastCustom.setDuration(Toast.LENGTH_SHORT);
                toastCustom.setGravity(Gravity.CENTER, 0, 0);
                toastCustom.show();
            }
        });
    }

    public void showToast(final int id) {
        runOnUiThread(new Runnable() {
            public void run() {
                String text = getString(id);
                Toast toastCustom = new Toast(context);
                TextView tv = new TextView(context);
                tv.setText(text);
                tv.setTextSize(20f);
                tv.setPadding(10, 2, 10, 2);
                tv.setGravity(Gravity.CENTER);
                tv.setTextColor(context.getResources().getColor(R.color.white));
                tv.setBackgroundResource(R.drawable.bg_toast);
                toastCustom.setView(tv);
                toastCustom.setDuration(Toast.LENGTH_SHORT);
                toastCustom.setGravity(Gravity.CENTER, 0, 0);
                toastCustom.show();
            }
        });
    }

    public void initHeadBar(int backShow, int titleShow, String titleText) {
        if (backShow == View.VISIBLE) {
            btn_back.setOnClickListener(this);
            btn_back.setVisibility(backShow);
        }
        if (titleShow == View.VISIBLE) {
            title.setText(titleText);
            title.setVisibility(titleShow);
        }
    }

    /**
     * @param activity
     */
    public void updateActivity(String sta, Class<?> activity) {
        Locale myLocale = new Locale(sta);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
    }

    public float[] getViewSize(View view) {

        return null;
    }

    protected void exit() {
        for (Activity ativity : ActivityManager) {
            ativity.finish();
        }
    }

    public void finishAll() {
        for (int i = 0; i < ActivityManager.size(); i++) {
            ActivityManager.get(i).finish();
        }
    }

    protected void updateToNewVersion() {

    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {
        static final String SYSTEM_REASON = "reason";
        static final String SYSTEM_HOME_KEY = "homekey";// home key
        static final String SYSTEM_RECENT_APPS = "recentapps";// long home key

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_REASON);
                if (reason != null) {
                    if (reason.equals(SYSTEM_HOME_KEY)) {
                    } else if (reason.equals(SYSTEM_RECENT_APPS)) {
                    }
                }
            }
        }
    }
}
